package com.kiloai.base.okgohttp;

import com.kiloai.base.AppContext;
import com.kiloai.base.okgohttp.callback.JsonCallback;
import com.kiloai.base.utils.SharedPreferenceHelper;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;

import java.io.File;
import java.util.Map;

/*
 * Created by david on 2018/3/6.
 */
public class HttpHandler {

    private static HttpHandler instance;
    private static String TAG;

    public static HttpHandler getInstance(String tag) {
        TAG = tag;
        if (instance == null) {
            synchronized (HttpHandler.class) {
                instance = new HttpHandler();
            }
        }
        return instance;
    }


    /**
     * post 请求
     * 不使用缓存
     *
     * @param url
     * @param jsonCallback
     */
    public void postRequest(String url, Map<String, String> map, JsonCallback jsonCallback) {
        OkGo.post(url)
                .tag(TAG)
                .headers("x-access-token", SharedPreferenceHelper.getHelper(AppContext.getInstance()).getStringByKey("token"))
                .params(map)
                .cacheMode(CacheMode.NO_CACHE)
                .execute(jsonCallback);
    }

    /**
     * post 请求
     * 带缓存 :先使用缓存，不管是否存在，仍然请求网络
     *
     * @param url
     * @param cacheKey
     * @param jsonCallback
     */
    public void postRequest(String url, String cacheKey, Map<String, String> map, JsonCallback jsonCallback) {
        OkGo.post(url)
                .tag(TAG)
                .headers("x-access-token", SharedPreferenceHelper.getHelper(AppContext.getInstance()).getStringByKey("token"))
                .params(map)
                .cacheKey(cacheKey)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .execute(jsonCallback);
    }

    /**
     * get 请求
     * 不使用缓存
     *
     * @param url
     * @param jsonCallback
     */
    public void getRequest(String url, JsonCallback jsonCallback) {
        OkGo.get(url)
                .tag(TAG)
                .headers("x-access-token", SharedPreferenceHelper.getHelper(AppContext.getInstance()).getStringByKey("token"))
                .cacheMode(CacheMode.NO_CACHE)
                .execute(jsonCallback);
    }

    /**
     * get 请求
     * 带缓存 :先使用缓存，不管是否存在，仍然请求网络
     *
     * @param url
     * @param cacheKey
     * @param jsonCallback
     */
    public void getRequest(String url, String cacheKey, JsonCallback jsonCallback) {
        OkGo.get(url)
                .tag(TAG)
                .headers("x-access-token", SharedPreferenceHelper.getHelper(AppContext.getInstance()).getStringByKey("token"))
                .cacheKey(cacheKey)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .execute(jsonCallback);
    }

    /**
     * put 请求
     * 不使用缓存
     *
     * @param url
     * @param jsonCallback
     */
    public void putRequest(String url, Map<String, String> map, JsonCallback jsonCallback) {
        OkGo.put(url)
                .tag(TAG)
                .headers("x-access-token", SharedPreferenceHelper.getHelper(AppContext.getInstance()).getStringByKey("token"))
                .params(map)
                .cacheMode(CacheMode.NO_CACHE)
                .execute(jsonCallback);
    }

    /**
     * put 请求
     * 带缓存 :先使用缓存，不管是否存在，仍然请求网络
     *
     * @param url
     * @param cacheKey
     * @param jsonCallback
     */
    public void putRequest(String url, String cacheKey, Map<String, String> map, JsonCallback jsonCallback) {
        OkGo.put(url)
                .tag(TAG)
                .headers("x-access-token", SharedPreferenceHelper.getHelper(AppContext.getInstance()).getStringByKey("token"))
                .params(map)
                .cacheKey(cacheKey)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .execute(jsonCallback);
    }

    /**
     * delete 请求
     * 不使用缓存
     *
     * @param url
     * @param jsonCallback
     */
    public void deleteRequest(String url, JsonCallback jsonCallback) {
        OkGo.delete(url)
                .tag(TAG)
                .headers("x-access-token", SharedPreferenceHelper.getHelper(AppContext.getInstance()).getStringByKey("token"))
                .cacheMode(CacheMode.NO_CACHE)
                .execute(jsonCallback);
    }

    /**
     * delete 请求
     * 带缓存 :先使用缓存，不管是否存在，仍然请求网络
     *
     * @param url
     * @param cacheKey
     * @param jsonCallback
     */
    public void deleteRequest(String url, String cacheKey,  JsonCallback jsonCallback) {
        OkGo.delete(url)
                .tag(TAG)
                .headers("x-access-token", SharedPreferenceHelper.getHelper(AppContext.getInstance()).getStringByKey("token"))
                .cacheKey(cacheKey)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .execute(jsonCallback);
    }

    /**
     * 上传
     * @param map
     * @param url
     * @param filePath
     * @param fileParamName
     * @param jsonCallback
     */
    public void postImage(Map<String, String> map, String url, String filePath, String fileParamName, JsonCallback jsonCallback) {
        OkGo.post(url)
                .tag(TAG)
                .headers("x-access-token", SharedPreferenceHelper.getHelper(AppContext.getInstance()).getStringByKey("token"))
                .params(map)
                .params(fileParamName, new File(filePath))
                .execute(jsonCallback);
    }
}
